<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/1/17 0017
 * Time: 9:45
 */

namespace core\SMS;

require_once EXTEND_PATH . 'core/Sms/aliyun-php-sdk-core/Config.php';
use Sms\Request\V20160927 as SMSS;

class SMS
{
    /**
     * 发送短信
     * @param $templateCode 模板代号
     * @param $recNum 手机号
     * @param $arg 变量数组
     * @param string $action 短信签名
     * @return bool
     */
    public static function send($templateCode, $recNum, $arg, $action = '')
    {
        $iClientProfile = \DefaultProfile::getProfile(config('sms.regionIds'), config('sms.accessId'), config('sms.accessKey'));
        $client = new \DefaultAcsClient($iClientProfile);
        $request = new SMSS\SingleSendSmsRequest();
        $signName = '超旅联票';
        if ($action != '') {
            $signName = '起步天使';
        }
        $request->setSignName($signName);/*签名名称*/
        $request->setTemplateCode($templateCode);/*模板code*/
        $request->setRecNum($recNum);/*目标手机号*/
        $request->setParamString(json_encode($arg));
        try {
            $response = $client->getAcsResponse($request);
            return true;
        }
        catch (\ClientException  $e) {
            $info = "code: " . $e->getErrorCode() . "\n" . "msg: " .$e->getErrorMessage() . "\n";
            Log::write($info, 'notice');
            return fase;
        }
        catch (\ServerException  $e) {
            $info = "code: " . $e->getErrorCode() . "\n" . "msg: " .$e->getErrorMessage() . "\n";
            Log::write($info, 'notice');
            return false;
        }
    }
}